/**   
* @Title: CaiWuXiangGuanView.java 
* @Package com.zjdiepu.www.manage.api.zpp.view 
* 
* @author zhangpp
* @date 2017年5月31日 下午5:19:01 
* @version V1.0   
*/
package com.zjdiepu.www.manage.api.zpp.view;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.UUIDFactory;
import com.zjdiepu.www.commpent.ViewScopeControllerHelper;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.cw.ICwRebateService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessLoanService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.ibiz.zpp.PublicService;
import com.zjdiepu.www.model.cw.dto.CwRebate;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.dto.YwBusinessCar;
import com.zjdiepu.www.model.yw.dto.YwBusinessLoan;
import com.zjdiepu.www.model.yw.response.YwBusinessDetail;
import com.zjdiepu.www.util.CommonUtil;
import com.zjdiepu.www.util.ExceptionHelper;

/**      
 * 项目名称：diepu-parent-manage   
 * 类名称：CaiWuXiangGuanView   
 * 类描述：   
 * 创建人：zhangpp
 * 创建时间：2017年5月31日 下午5:19:01   
 * 修改人：zhangpp
 * 修改时间：2017年5月31日 下午5:19:01   
 * 修改备注：   
 * @version V1.0     
 */
@Controller
@RequestMapping(value= "CaiWuXiangGuanView", produces="application/json;charset=UTF-8")
@Api(tags = {"作者：张鹏鹏，电话：15158822934"}, produces="application/json;charset=UTF-8")
public class CaiWuXiangGuanView 
{
	@Resource
    private ViewScopeControllerHelper viewScopeControllerHelper;
	@Resource
    private ICachedDictionaryService cachedDictionaryService;
	@Resource
	private IYwBusinessLoanService iYwBusinessLoanService;
	@Resource
    private ICwRebateService cwRebateService;
	@Resource
	private IYwBusinessService ywBusinessService;
	@Resource
	private PublicService publicService;
	
	@ApiOperation(value = "登记业务提成", response = Boolean.class, httpMethod="GET", tags={"登记业务提成"})
    @RequestMapping(value = "getDengJiYwTiChengCountView", method = RequestMethod.GET)
    public String getDengJiYwTiChengCountView(Model model,String busiId) {
		
		if (StringUtils.isBlank(busiId)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
		
		model.addAttribute("carCategoryType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.CAR_CATEGORY,"请选择..."));
    	model.addAttribute("carColorType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.CAR_COLOR,"请选择..."));
    	
    	viewScopeControllerHelper.preparedNowTime(model);
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(busiId, model);
    	YwBusinessCar car = viewScopeControllerHelper.preparedCar(busiId, null,model);
    	if(car != null) {
    		viewScopeControllerHelper.preparedLoan(busiId, car.getId(), model,car.getCarType());
    	}
    	if(ywBusinessDetail != null) {
    		viewScopeControllerHelper.preparedHistoryBusiness(ywBusinessDetail.getCustomerNo(), model);
    	}
    	viewScopeControllerHelper.preparedRegin(model,null,null);
    	viewScopeControllerHelper.preparedCarModel(model);
    	viewScopeControllerHelper.preparedAttachmentEnum(model);
    	
    	//车辆信息
    	YwBusinessLoan ywBusinessLoan = new YwBusinessLoan();
    	ywBusinessLoan.setBusiId(busiId);
    	ywBusinessLoan.setDpStatus(1);
    	ywBusinessLoan = iYwBusinessLoanService.selectRefined(ywBusinessLoan);
    	
    	//返利信息
    	CwRebate cwRebate = new CwRebate();
    	cwRebate.setBusiId(busiId);
    	cwRebate.setDpStatus(1);
    	cwRebate = cwRebateService.selectRefined(cwRebate);
		
		model.addAttribute("busiId", busiId);
		model.addAttribute("ywBusinessLoan", ywBusinessLoan);
		model.addAttribute("cwRebate", cwRebate);
		
    	return "caiWuXiangGuan/dengjiYWticheng";
    }
	
	
	/**
	* 财务相关/客户退押金 
	* @author zhangpp
	* @Title: getDaiHouYewTuikuan 
	* 
	* @param @param model
	* @param @param busi_id
	* @param @param id
	* @param @return    设定文件 
	* @return String    返回类型 
	* @version V1.0
	* @throws
	 */
	@ApiOperation(value = "财务相关/客户退押金", response = Boolean.class, httpMethod="GET", tags={"财务相关/客户退押金"})
    @RequestMapping(value = "getDaiHouYewTuikuan", method = RequestMethod.GET)
    public String getDaiHouYewTuikuan(Model model,String busiId,String id) {
		
		if (StringUtils.isBlank(busiId)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
    	model.addAttribute("carCategoryType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.CAR_CATEGORY,"请选择..."));
    	model.addAttribute("carColorType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.CAR_COLOR,"请选择..."));
    	
    	viewScopeControllerHelper.preparedNowTime(model);
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(busiId, model);
    	YwBusinessCar car = viewScopeControllerHelper.preparedCar(busiId, null,model);
    	if(car != null) {
    		viewScopeControllerHelper.preparedLoan(busiId, car.getId(), model, car.getCarType());
    	}
    	if(ywBusinessDetail != null) {
    		viewScopeControllerHelper.preparedHistoryBusiness(ywBusinessDetail.getCustomerNo(), model);
    	}
    	viewScopeControllerHelper.preparedRegin(model,null,null);
    	viewScopeControllerHelper.preparedCarModel(model);
    	viewScopeControllerHelper.preparedAttachmentEnum(model);
		
		//判断条件  附件上传
    	viewScopeControllerHelper.preparedAttachmentEnum(model);
		
		model.addAttribute("busiId", busiId);
		model.addAttribute("uuid", UUIDFactory.getUUID());
		model.addAttribute("id", id);
		
    	return "caiWuXiangGuan/customerMonReturn";
    }
	
	/**
	* 财务相关/结清单确认
	* @author zhangpp
	* @Title: getKeHuJieQingDanListView 
	* 
	* @param @param model
	* @param @param busi_id
	* @param @param id
	* @param @return    设定文件 
	* @return String    返回类型 
	* @version V1.0
	* @throws
	 */
	@ApiOperation(value = "财务相关/结清单确认", response = Boolean.class, httpMethod="GET", tags={"业务受理/弃贷"})
    @RequestMapping(value = "getKeHuJieQingDanListView", method = RequestMethod.GET)
    public String getKeHuJieQingDanListView(Model model,String busiId) {
		
		if (StringUtils.isBlank(busiId)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
    	model.addAttribute("carCategoryType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.CAR_CATEGORY,"请选择..."));
    	model.addAttribute("carColorType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.CAR_COLOR,"请选择..."));
    	
    	viewScopeControllerHelper.preparedNowTime(model);
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(busiId, model);
    	YwBusinessCar car = viewScopeControllerHelper.preparedCar(busiId, null,model);
    	if(car != null) {
    		viewScopeControllerHelper.preparedLoan(busiId, car.getId(), model, car.getCarType());
    	}
    	if(ywBusinessDetail != null) {
    		viewScopeControllerHelper.preparedHistoryBusiness(ywBusinessDetail.getCustomerNo(), model);
    	}
    	viewScopeControllerHelper.preparedRegin(model,null,null);
    	viewScopeControllerHelper.preparedCarModel(model);
    	viewScopeControllerHelper.preparedAttachmentEnum(model);
		
    	
		model.addAttribute("busiId", busiId);
    	return "daiHouYw/customerjieqing";
    }
	
	/**
	*  客户结清转发
	*  @author  zhangpp
	*  @Title:  getKeHuJieQingView  
	*    
	*  @param  @param model
	*  @param  @param busiId
	*  @param  @param cusId
	*  @param  @return        设定文件  
	*  @return  String        返回类型  
	*  @version  V1.0
	*  @throws
	 */
	@RequestMapping(value = "getKeHuJieQingView", method = RequestMethod.GET)
    public String getKeHuJieQingView(Model model,String busiId,String cusId)
	{
		
		if (StringUtils.isBlank(busiId)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
		
		YwBusiness ywBusiness = ywBusinessService.selectById(busiId);
		if(null == ywBusiness)
		{
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
		
		String sqlXmls = "select max(t.overdue_count_continuous) overdue_count_continuous,max(t.overdue_count_total)overdue_count_total  from dh_overduelist_history t where t.busi_id = '"+busiId+"' and t.dp_status = 1";
		List<Map<String, String>> list = publicService.queryWhereSql(sqlXmls);
		Map<String, String> object = list.get(0);
		JSONObject objs = CommonUtil.getJSONObject(object);
		int overdueCountContinuous = 0;
		int overdueCountTotal = 0;
		
		
		if(StringUtils.isNotBlank(objs.getString("overdue_count_continuous")))
		{
			overdueCountContinuous = objs.getInt("overdue_count_continuous");
		}
		
		if(StringUtils.isNotBlank(objs.getString("overdue_count_total")))
		{
			overdueCountTotal = objs.getInt("overdue_count_total");
		}
		
		model.addAttribute("overdueCountContinuous", overdueCountContinuous);
		model.addAttribute("overdueCountTotal", overdueCountTotal);
		model.addAttribute("busiId", busiId);
		model.addAttribute("queryCreditApplyId", ywBusiness.getQueryCreditApplyId());
		model.addAttribute("cusId", cusId);
    	return  "system/loanAfter/applySettlement";
    }
}
